package com.xyh.flowershop.controller;

import com.xyh.flowershop.annotations.RefreshToken;
import com.xyh.flowershop.constant.CommonConstant;
import com.xyh.flowershop.service.OrderService;
import com.xyh.flowershop.vo.req.orders.OrderAddReqVo;
import com.xyh.flowershop.vo.req.orders.OrderPageReqVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import com.xyh.flowershop.vo.resp.order.OrderDetailRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 小约翰可汗
 * @className OrderController
 * @createTime 2024/5/1221:37
 */
@Slf4j
@RefreshToken
@RestController
@RequestMapping("/order/")
@CrossOrigin(origins = "*", maxAge = 3600)
@Tag(name = "订单接口")
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @Operation(description = "提交订单")
    @PostMapping("add")
    public ResultRespVo addOrder(@RequestBody OrderAddReqVo vo){
        return ResultRespVo.success(orderService.addOrder(vo));
    }

    @Operation(description = "删除订单")
    @DeleteMapping("delete/{id}")
    public ResultRespVo deleteOrder(@PathVariable String id){
        List<String> ids = new ArrayList<>();
        ids.add(id);
        return ResultRespVo.success(orderService.deleteOrders(ids));
    }

    @Operation(description = "批量删除订单")
    @DeleteMapping("delete/batch")
    public ResultRespVo deleteBatchOrder(@RequestBody List<String> ids){
        return ResultRespVo.success(orderService.deleteOrders(ids));
    }

    @Operation(description = "取消订单")
    @GetMapping("cancel/{id}")
    public ResultRespVo cancelOrder(@PathVariable String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        orderService.cancelOrders(ids);
        return ResultRespVo.success(CommonConstant.SUCCESS);
    }

    @Operation(description = "分页查询订单")
    @PostMapping("find/page")
    public ResultRespVo findPageOrders(@RequestBody OrderPageReqVo vo){
        return ResultRespVo.success(orderService.findPageOrders(vo));
    }

    @Operation(description = "查询订单详情")
    @GetMapping("find/{id}")
    public ResultRespVo findOrderDetail(@PathVariable String id){
        OrderDetailRespVo orderDetail = orderService.findOrderDetail(id);
        return ResultRespVo.success(orderDetail);
    }

    @Operation(description = "确认收货")
    @GetMapping("confirm/receipt/{id}")
    public ResultRespVo confirmReceipt(@PathVariable String id) {
        return orderService.confirmReceipt(id) ? ResultRespVo.success("确认收货成功") : ResultRespVo.error("确认收货失败");
    }

    @Operation(description = "再来一单")
    @GetMapping("buy/again/{id}")
    public ResultRespVo buyAgain(@PathVariable String id) {
        Boolean result = orderService.buyAgain(id);
        return ResultRespVo.success(result);
    }
}
